package net.bingosoft.common.struts.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.bingosoft.common.BeanHelper;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.struts.taglib.TagUtils;

/**
 * 2007-8-29 下午02:43:55
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:查看Bean中属性的Tag
 * ChangeLog:
 */
public class BeanTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String property;
	private String name = "org.apache.struts.taglib.html.BEAN";
	private String scope = "request";

	public String getProperty() {
		return property;
	}
	public void setProperty(String property) {
		this.property = property;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String bean) {
		this.name = bean;
	}
	
	public String getScope() {
		return scope;
	}
	public void setScope(String scope) {
		this.scope = scope;
	}
	public int doEndTag() throws JspException {
		try {
			if("formName".equals(property)){
				property = "class.name";
			}
			Object targetBean = "";
			try{
				targetBean = TagUtils.getInstance().lookup(pageContext, name, property, scope);
				if(targetBean == null) targetBean = "No bean with name [" + name + "] exists in scope " + scope;
				if(StringUtils.isEmpty(property)){
					targetBean = ToStringBuilder.reflectionToString(targetBean);
				}
			}catch(Exception ex){
				targetBean = ex.getMessage();
			}
			pageContext.getOut().write("<dl><dt><code>" + BeanHelper.converToString(targetBean) + "</code></dt></dl>");
		} catch (Exception e) {
			throw new JspException("Exception: " + e.getMessage());
		}
		return EVAL_PAGE;
	}


}
